package com.dianxun.exam.adapter.provider;

import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.dianxun.exam.R;
import com.dianxun.exam.common.AppKeyManager;
import com.dianxun.exam.entity.ChoiceListBean;
import com.dianxun.exam.entity.ItemListBean;
import com.dianxun.exam.entity.TypeEntity;
import com.dianxun.exam.utils.Utils;

/**
 * 多选题
 */
public class MultipleChoiceProvider extends BaseItemProvider<TypeEntity,BaseViewHolder> {

    @Override
    public int viewType() {
        return AppKeyManager.TYPE_MULTIPLE_CHOICE;
    }

    @Override
    public int layout() {
        return R.layout.item_exam_multiple_choice;
    }

    @Override
    public void convert(BaseViewHolder helper, TypeEntity data, int position) {
        ChoiceListBean entity = (ChoiceListBean) data;
        helper.setText(R.id.tv_title, (position - 2) + ".  " + entity.getItemName() + "（" + entity.getScore() + "分）");
        LinearLayout rg = helper.getView(R.id.layout_multiple);
        if (rg.getChildCount() == 0) {
            for (int i = 0; i < entity.getItemList().size(); i++) {
                final ItemListBean itemListBean = entity.getItemList().get(i);
                CheckBox checkBox = new CheckBox(mContext);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                layoutParams.setMargins(0, 10, 0, 0);
                checkBox.setLayoutParams(layoutParams);
                checkBox.setText(Utils.getNo(i) + itemListBean.getItemName());
                checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (isChecked) {
                            itemListBean.setAnswer(1);
                        } else {
                            itemListBean.setAnswer(0);
                        }
                    }
                });
                if (itemListBean.getAnswer() == 1) {
                    checkBox.setChecked(true);
                } else {
                    checkBox.setChecked(false);
                }
                rg.addView(checkBox);
            }
        }
    }

    @Override
    public void onClick(BaseViewHolder helper, TypeEntity data, int position) {

    }

    @Override
    public boolean onLongClick(BaseViewHolder helper, TypeEntity data, int position) {
        return true;
    }
}
